package bbs.web.filter;

import java.io.*; 
import java.util.*;

import javax.servlet.*; 
import javax.servlet.annotation.*;
import javax.servlet.http.*;

import org.apache.logging.log4j.*; 

import bbs.web.control.WebController;
/**
 * Servlet Filter implementation class JspFilter
 */
@WebFilter(dispatcherTypes = {DispatcherType.REQUEST } , urlPatterns = { "*.con" })
public class JspFilter implements Filter {
	
	/**
	 * Log4j logger
	 */
	static final Logger logger = LogManager.getLogger(JspFilter.class);

    /**
     * Default constructor. 
     */
    public JspFilter() { 
    }

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() { 
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		
		// place your code here
		
		boolean debug = false ; 
		String qryStr = "";
		HttpServletRequest hRequest = (HttpServletRequest) request ; 
		String reqUrl = hRequest.getRequestURL().toString() ; 
		qryStr = hRequest.getQueryString();
		qryStr = qryStr == null ? "" : qryStr; 
		
		String contextPath = hRequest.getContextPath();
		
		String reqUrlFull = "";
		String reqUrlFullEncoded = "";
		if( true ) {
			int fi = reqUrl.indexOf( contextPath ) + contextPath.length() ; 
			
			reqUrlFull = reqUrl.substring( fi );
			
			if( reqUrlFull.startsWith( "/") ) {
				if( reqUrlFull.length() > 0 ) { 
					reqUrlFull = reqUrlFull.substring( 1 );
				} else if( reqUrlFull.length() < 1 ) {
					reqUrlFull = "";
				}
			}
			
			if( qryStr != null && qryStr.length() > 0 ) {
				reqUrlFull += "?" + qryStr;
			}
			
			reqUrlFullEncoded = WebController.getStringUrlEncoded( reqUrlFull );
		}
		
		if( debug ) { 
			logger.trace( "Request contextPath : " + contextPath );
			logger.trace( "Request URL : " + reqUrl );
			logger.trace( "Request Query String : " + qryStr ); 
			
			logger.trace( "reqUrlFull : " + reqUrlFull );
			logger.trace( "reqUrlFull Encoded : " + reqUrlFullEncoded );
		}
		
		request.setCharacterEncoding("UTF-8");  
		response.setCharacterEncoding("UTF-8");
		
		request.setAttribute( "reqUrlFull", reqUrlFullEncoded );
		
		response.setContentType( "text/html; charset=UTF-8" );
		
		if( debug ) {  
			// 파라미터 값 분석
			Map<String, String[] > map = request.getParameterMap();
			
			if( map != null && map.size() > 0 ) {
				Set<String> keys = map.keySet();
				Iterator<String> keysIt = keys.iterator();
				String key ;
				String [] vals;
				while( keysIt.hasNext() ) {
					key = keysIt.next();
					vals = map.get( key );
					if( vals == null || vals.length < 1 ) {
						logger.trace( "key: " + key + " = " + ";" );
					} else { 
						for( int i = 0, iLen = vals.length ; i < iLen ; i ++ ) {
							logger.trace( "key: " + key + " = " + vals[i] + ";" );
						}
					}
				}
			}
		}
		
		// end of placing your code here

		// pass the request along the filter chain
		chain.doFilter(request, response);
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {  
		
	}

}
